/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.senac.controle;

import br.com.senac.dao.ClienteDAO;
import br.com.senac.dao.ClienteDAOImp;
import br.com.senac.entidades.Cliente;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Alessandra
 */
@WebServlet(name = "ControleCliente", urlPatterns = {"/ControleCliente"})
public class ControleCliente extends HttpServlet {

        /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    private HttpServletRequest request;
    private HttpServletResponse response;
    private Cliente cliente;
    private RequestDispatcher rd;

    protected void processRequest()
            throws ServletException, IOException {

        String cmd = request.getParameter("cmd");

        switch (cmd) {
            case "salvar":
                salvar();
                break;
            case "listar":
                listar();
                break;
            case "excluir":
                excluir();
                break;
            case "pesqCliente":
                pesquisaCliente();
                break;

            case "alterar":
                alterar();
                break;
        }

    }

    private void salvar() {
        ClienteDAO cliDAO = new ClienteDAOImp();
        try {
            cliDAO.salvar(carregaCliente());
            listar();
        } catch (Exception ex) {
            Logger.getLogger(ControleCliente.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void listar() {
        ClienteDAO cliDAO = new ClienteDAOImp();

        try {
            List clientes = cliDAO.listar();
            request.setAttribute("listaClientes", clientes);
            rd = request.getRequestDispatcher("mostraClientes.jsp");
            rd.forward(request, response);
        } catch (Exception ex) {
            Logger.getLogger(ControleCliente.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private void excluir() {
        ClienteDAO cliDAO = new ClienteDAOImp();
        String idCmd = request.getParameter("id");
        int id = Integer.parseInt(idCmd);
        try {
            cliDAO.excluir(id);
            listar();
        } catch (Exception ex) {
            Logger.getLogger(ControleCliente.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void pesquisaCliente() {
        ClienteDAO  cliDAO = new ClienteDAOImp();
        String idForm = request.getParameter("id");
        int id = Integer.parseInt(idForm);

        try {
            cliente = (Cliente) cliDAO.procurar(id);
            request.setAttribute("cliente", cliente);
            rd = request.getRequestDispatcher("formAtuCliente.jsp");
            rd.forward(request, response);
        } catch (Exception ex) {
            Logger.getLogger(ControleCliente.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private void alterar() {
        ClienteDAO cliDAO = new ClienteDAOImp();
        try {
            cliDAO.alterar(carregaCliente());
            listar();
        } catch (Exception ex) {
            Logger.getLogger(ControleSituacaoReserva.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private Cliente carregaCliente() {
        cliente = new Cliente();
        String id = request.getParameter("id");
        if (id != null) {
            cliente.setId(Integer.parseInt(id));
        }
        cliente.setNome(request.getParameter("nome"));
        cliente.setCodigo(request.getParameter("codigo"));
        cliente.setRg(request.getParameter("rg"));
        cliente.setCpf(request.getParameter("cpf"));
        cliente.setSexo(request.getParameter("sexo"));
//        cliente.setDataNascimento(request.getParameter("dataNascimento"));
        cliente.setDependente(Integer.parseInt(request.getParameter("dependente")));
        cliente.setEmail(request.getParameter("email"));
        cliente.setTelefone(request.getParameter("telefone"));
        cliente.setCelular(request.getParameter("celular"));
        
        return cliente;
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        this.request = request;
        this.response = response;

        processRequest();
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        this.request = request;
        this.response = response;

        processRequest();
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
